package test01111;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private List<Contact> contacts;

    public static void main(String[] args) throws IOException {
        new Server().start();
    }

    public Server() {
        contacts = new ArrayList<>();
        contacts.add(new Contact("张三", "北京市朝阳区", "010-1234567"));
        contacts.add(new Contact("李四", "北京市海淀区", "010-2345678"));
    }

    public void start() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8000);
        System.out.println("服务器已启动...");

        while (true) {
            Socket socket = serverSocket.accept();
            new Thread(() -> {
                try {
                    handle(socket);
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    private void handle(Socket socket) throws IOException, ClassNotFoundException {
        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());

        while (true) { // 处理多个客户端请求
            String command = inputStream.readUTF();
            switch (command) {
                case "list":
                    outputStream.writeObject(contacts);
                    break;
                case "add":
                    Contact contact = (Contact) inputStream.readObject();
                    contacts.add(contact);
                    outputStream.writeBoolean(true);
                    break;
                case "delete":
                    int index = inputStream.readInt();
                    if (index >= 0 && index < contacts.size()) {
                        contacts.remove(index);
                        outputStream.writeBoolean(true);
                    } else {
                        outputStream.writeBoolean(false);
                    }
                    break;
                case "update":
                    index = inputStream.readInt();
                    if (index >= 0 && index < contacts.size()) {
                        contact = (Contact) inputStream.readObject();
                        contacts.set(index, contact);
                        outputStream.writeBoolean(true);
                        outputStream.writeObject(contacts);  // 在更新操作完成后立即刷新联系人列表
                        outputStream.flush();
                    } else {
                        outputStream.writeBoolean(false);
                    }
                    break;
                case "exit": // 客户端请求退出
                    inputStream.close();
                    outputStream.close();
                    socket.close();
                    return;
            }
        }
    }


}
